<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model\LazyCommon;
use App\Base\BaseConfig;
use App\Base\BaseModel;
use BaseExt\MyCurl;

class WechatTemplateMessageModel extends BaseModel{

    public $AppId; //服务号APPId
    public $AppSecret; //服务号APPId

    public $Code;
    public $AccessToken;
    public $JsApiTicket;
    public $Openid; //商户appid下，用户的openid
    public $url;//请求连接地址
    public $curl_timeout;//请求超时时间
    public $response;//接口返回信息

    public function __construct($doctrine)
    {
        parent::__construct($doctrine);
        $this->AppId = BaseConfig::__WECHAT_PLAT_APPID__;
        $this->AppSecret = BaseConfig::__WECHAT_PLAT_APPSEC__;
    }



    public function sendTemplateMessage($toUser , $title,$keyword1,$keyword2,$remark,$urlDeal="")
    {
        $WechatModel = new WechatModel($this->getDoctrine());
        if (!$ACCESS_TOKEN = $WechatModel->getAccessToken()) {
            die;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$ACCESS_TOKEN";
        $MyCurl = new MyCurl();
        if (!$toUser) {
            $OPENID = "oQ_Fh1uqeyXRmTweTA2zoTh_sxqg";
        }else{
            $OPENID = $toUser;
        }
        $data = '{
           "touser":"'.$OPENID.'",
           "template_id":"iGYrkX6d09klYKnQSzGJfR9qBztQxXuXOfCh-805cD4",
           "url":"'.$urlDeal.'",
	       "topcolor": "#FF0000",
           "data":{
               "first": {
                   "value":"'.$title.'",
                   "color":"#173177"
               },
               "keyword1":{
                   "value":"'.$keyword1.'",
                   "color":"#173177"
               },
               "keyword2": {
                   "value":"'.$keyword2.'",
                   "color":"#173177"
               },
               "remark":{
                   "value":"'.$remark.'",
                   "color":"#173177"
               }
           }
       }';
        $MyCurl->post($url,$data);
    }

    //库存不足提示
    public function productSellOut($toUser , $title,$keyword1,$keyword2,$remark,$urlDeal="")
    {
        $WechatModel = new WechatModel($this->getDoctrine());
        if (!$ACCESS_TOKEN = $WechatModel->getAccessToken()) {
            die;
        }
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$ACCESS_TOKEN";
        $MyCurl = new MyCurl();
        if (!$toUser) {
            $OPENID = "oQ_Fh1uqeyXRmTweTA2zoTh_sxqg";
        }else{
            $OPENID = $toUser;
        }
        $data = '{
           "touser":"'.$OPENID.'",
           "template_id":"XZWyNq0UOwRsqExoc3TMoySE_MMA3ndXE-bO2JXj8YE",
           "url":"'.$urlDeal.'",
	       "topcolor": "#FF0000",
           "data":{
               "first": {
                   "value":"'.$title.'",
                   "color":"#173177"
               },
               "keyword1":{
                   "value":"'.$keyword1.'",
                   "color":"#173177"
               },
               "keyword2": {
                   "value":"'.$keyword2.'",
                   "color":"#173177"
               },
               "keyword3": {
                   "value":"'.date(YYMMDDHHIISS).'",
                   "color":"#173177"
               },
               "remark":{
                   "value":"'.$remark.'",
                   "color":"#173177"
               }
           }
       }';
        $MyCurl->post($url,$data);
    }
}